package icu.zlz.emqx.controller;

import java.util.List;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

import icu.zlz.emqx.domain.IotGroup;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import icu.zlz.common.log.annotation.Log;
import icu.zlz.common.log.enums.BusinessType;
import icu.zlz.common.security.annotation.RequiresPermissions;
import icu.zlz.emqx.domain.IotTopic;
import icu.zlz.emqx.service.IIotTopicService;
import icu.zlz.common.core.web.controller.BaseController;
import icu.zlz.common.core.web.domain.AjaxResult;
import icu.zlz.common.core.utils.poi.ExcelUtil;
import icu.zlz.common.core.web.page.TableDataInfo;

/**
 * 主题Controller
 *
 * @author zangs
 * @since 2024-12-31
 */
@RestController
@RequestMapping("/topic")
public class IotTopicController extends BaseController {
    @Autowired
    private IIotTopicService iotTopicService;


    @RequiresPermissions("emqx:topic:list")
    @GetMapping("/groupList/{name}")
    public AjaxResult groupList(@PathVariable("name") String name) {
        return AjaxResult.success(iotTopicService.selectIotGroupList(name));

    }

    /**
     * 查询主题列表
     */
    @RequiresPermissions("emqx:topic:list")
    @GetMapping("/list")
    public TableDataInfo list(IotTopic iotTopic) {
        startPage();
        List<IotTopic> list = iotTopicService.selectIotTopicList(iotTopic);
        return getDataTable(list);
    }

    /**
     * 导出主题列表
     */
    @RequiresPermissions("emqx:topic:export")
    @Log(title = "主题", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, IotTopic iotTopic) {
        List<IotTopic> list = iotTopicService.selectIotTopicList(iotTopic);
        ExcelUtil<IotTopic> util = new ExcelUtil<IotTopic>(IotTopic.class);
        util.exportExcel(response, list, "主题数据");
    }

    /**
     * 获取主题详细信息
     */
    @RequiresPermissions("emqx:topic:query")
    @GetMapping(value = "/{topicId}")
    public AjaxResult getInfo(@PathVariable("topicId") Long topicId) {
        return success(iotTopicService.selectIotTopicByTopicId(topicId));
    }

    /**
     * 新增主题
     */
    @RequiresPermissions("emqx:topic:add")
    @Log(title = "主题", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody IotTopic iotTopic) {
        return toAjax(iotTopicService.insertIotTopic(iotTopic));
    }

    /**
     * 修改主题
     */
    @RequiresPermissions("emqx:topic:edit")
    @Log(title = "主题", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody IotTopic iotTopic) {
        return toAjax(iotTopicService.updateIotTopic(iotTopic));
    }

    /**
     * 删除主题
     */
    @RequiresPermissions("emqx:topic:remove")
    @Log(title = "主题", businessType = BusinessType.DELETE)
    @DeleteMapping("/{topicIds}")
    public AjaxResult remove(@PathVariable Long[] topicIds) {
        return toAjax(iotTopicService.deleteIotTopicByTopicIds(topicIds));
    }
}
